from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
from django.urls import reverse

from .models import BlogArticles
from django.http import HttpResponseRedirect


# 判断用户有没有登录 没有就跳转到登录界面
@login_required(login_url='/account/login/')
def blog_title(request):
    # 到数据库查询数据
    blogs = BlogArticles.objects.all()
    # 组织数据
    context = {
        "blogs": blogs
    }
    # 传递数据给模板
    return render(request, "blog/titles.html", context=context)


def blog_article(request, article_id):
    # article = BlogArticles.objects.get(id=article_id)
    # 返回404界面
    article = get_object_or_404(BlogArticles, id=article_id)
    pub = article.publish
    return render(request, "blog/content.html", {"article": article, "publish": pub})
